﻿using System.Collections.Generic;
using System.Linq;
using System.Xml;
using SVTTranslateTextEngine.Core;
using SVTTranslateTextEngine.Entities;

namespace SVTTranslateTextEngine
{
    /// <summary>
    /// The language support access to information object
    /// </summary>
    public class LangHelper
    {
        /// <summary>
        /// the  file relative path of setting about which language allow to translate .
        /// </summary>
        private const string LANG_SETTTING_XML_FILE_PATH = "SupportLanguageSetting.xml";

        private readonly FileAccessHelper file_access_helper = new FileAccessHelper();

        /// <summary>
        /// Get the support language information from the XML setting file .
        /// </summary>
        /// <returns>array of the support language info objects </returns>
        public List<SupportLanguageInfo> GetSupportLangInfoFromXml()
        {
            List<SupportLanguageInfo> result = null;
            var xmlDoc = new XmlDocument();
            string xmlContext = file_access_helper.ReadTextContext(LANG_SETTTING_XML_FILE_PATH);
            if (!string.IsNullOrEmpty(xmlContext))
            {
                xmlDoc.LoadXml(xmlContext.ToLower());
                XmlNodeList nodes = xmlDoc.SelectNodes(
                    ("settings/support_engines/support_engine").ToLower());
                if (nodes != null && nodes.Count > 0)
                    result =
                        nodes.Cast<XmlNode>().Select(supportLangNode => new SupportLanguageInfo(supportLangNode)).Where(
                            langInfo => langInfo != null).ToList();
            }
            return result;
        }


        /// <summary>
        /// Get the support language info object by the vendor name of the translate engine supplier . eg : GOOGLE , BINGE 
        /// </summary>
        /// <param name="szVendorName">the name of the translation engine supplier</param>
        /// <returns>the support language info object , null means the vendor is not exist</returns>
        public SupportLanguageInfo GetSupportLangInfoByVendorName(string szVendorName)
        {
            SupportLanguageInfo result = null;
            var xmlDoc = new XmlDocument();
            string xmlContext = file_access_helper.ReadTextContext(LANG_SETTTING_XML_FILE_PATH);
            if (!string.IsNullOrEmpty(xmlContext))
            {
                xmlDoc.LoadXml(xmlContext.ToLower());
                XmlNode supportLangNode = xmlDoc.SelectSingleNode(
                    ("settings/support_engines/support_engine[name='" + szVendorName + "']").ToLower());
                result = new SupportLanguageInfo(supportLangNode);
            }
            return result;
        }


        /// <summary>
        /// update the information of language supportive.
        /// </summary>
        /// <param name="szXmlContext">context of the xml</param>
        /// <returns>Return the update is success or not ?</returns>
        public static bool UpdateSupportLangInfoToXml(string szXmlContext)
        {
            return true;
        }
    }
}